package ar.edu.itba.bundundu;

import java.io.IOException;
import java.util.Date;
import java.util.ResourceBundle;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookParam;

public class RsvpServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    doPost(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
      RequestDispatcher d;
      String respuesta = (String)req.getParameter("answer");
      Long eid = Long.valueOf((String)req.getParameter("eid"));
      int er=0;
      if(respuesta!=null && !respuesta.equals("")){
        Facebook fb = new Facebook(req.getParameter(FacebookParam.SESSION_KEY.toString()));
        ResourceBundle rs = ResourceBundle.getBundle("config");
        String apikey = rs.getString("application.key");
        try {
          fb.getClient().events_rsvp(eid, respuesta);
        } catch (FacebookException e) {
            req.setAttribute("err", "The event couldn't be RSVPed. Why? In order to be able to RSVP an event for you, Bundundu must have an extended permission from you. Please, grant the extended permission before RSVPing the event<br>");
            er=1;
        }
      }
      
      
      if(er==0){
        req.setAttribute("success", "The event has been RSVPed. <br>");
      }
      d = getServletContext().getRequestDispatcher("/events");
      d.forward(req, resp);
  }

}
